var whiteMaterial = new THREE.MeshBasicMaterial({
   color: new THREE.Color(1,1,1),
});
var yellowMaterial = new THREE.MeshBasicMaterial({
    color: new THREE.Color(1,1,0),
});
var redMaterial = new THREE.MeshBasicMaterial({
    color: new THREE.Color(1,0,0),
});
var orangeMaterial = new THREE.MeshBasicMaterial({
    color: new THREE.Color(1,0.5,0),
});
var blueMaterial = new THREE.MeshBasicMaterial({
    color: new THREE.Color(0,0,1),
});
var greenMaterial = new THREE.MeshBasicMaterial({
    color: new THREE.Color(0,1,0),
});

var geometry = new THREE.PlaneGeometry(1,1);
var planeMesh1 = new THREE.Mesh(geometry,whiteMaterial);
planeMesh1.rotateX(Math.PI/2);
planeMesh1.position.set(0,-0.5,0);

var planeMesh2 = new THREE.Mesh(geometry,yellowMaterial);
planeMesh2.rotateX(-Math.PI/2);
planeMesh2.position.set(0,0.5,0);

var planeMesh3 = new THREE.Mesh(geometry,redMaterial);
planeMesh3.rotateX(0);
planeMesh3.position.set(0,0,0.5);

var planeMesh4 = new THREE.Mesh(geometry,orangeMaterial);
planeMesh4.rotateX(Math.PI);
planeMesh4.position.set(0,0,-0.5);

var planeMesh5 = new THREE.Mesh(geometry,blueMaterial);
planeMesh5.rotateY(-Math.PI/2);
planeMesh5.position.set(-0.5,0,0);

var planeMesh6 = new THREE.Mesh(geometry,greenMaterial);
planeMesh6.rotateY(Math.PI/2);
planeMesh6.position.set(0.5,0,0);

class Block{
    constructor(param){
        param = param || {
            x: 0,
            y: 0,
            z: 0
        };

        this.x = param.x;
        this.y = param.y;
        this.z = param.z;

        this.mesh = new THREE.Group();
        this.mesh.position.set(this.x,this.y,this.z);
        this.initMesh();
    }

    initMesh(){
        let mesh;

        if(this.y<0){
            mesh = planeMesh1.clone();
            mesh.color = 'white';
            this.mesh.add(mesh);
        }

        if(this.y>0){
            mesh = planeMesh2.clone();
            mesh.color = 'yellow';
            this.mesh.add(mesh);
        }

        if(this.z>0){
            mesh = planeMesh3.clone();
            mesh.color = 'red';
            this.mesh.add(mesh);
        }

        if(this.z<0){
            mesh = planeMesh4.clone();
            mesh.color = 'orange';
            this.mesh.add(mesh);
        }

        if(this.x<0){
            mesh = planeMesh5.clone();
            mesh.color = 'blue';
            this.mesh.add(mesh);
        }

        if(this.x>0){
            mesh = planeMesh6.clone();
            mesh.color = 'green';
            this.mesh.add(mesh);
        }

    }

    hasColor(color){
        for(let k=0; k<this.mesh.children.length; k++){
            if(this.mesh.children[k].color === color){
                return this.mesh.children[k];
            }
        }
        return null;
    }

    getPositionColor(v3){
        for(let k=0; k<this.mesh.children.length; k++){
            let pos = this.mesh.children[k].getWorldPosition();

            if(this.nearTo(pos.x, v3.x)&&this.nearTo(pos.y, v3.y)&&this.nearTo(pos.z, v3.z)){
                return this.mesh.children[k].color;
            }
        }
        return null;
    }

    nearTo(a,b) {
        if(Math.abs(a-b)<0.1){
            return true;
        }
        return false;
    }

}